#include "sobelfilter.h"


struct _sobel_arr SobelFilter::sobel_arr(QImage *qin){
    in_image=qin;
    struct _sobel_arr sob_sob_arr;
    k_cols=3;
    k_rows=3;
    kernel=new double[k_rows*k_cols];
    kernel[0]=1.0;
    kernel[1]=2.0;
    kernel[2]=1.0;
    kernel[3]=0.0;
    kernel[4]=0.0;
    kernel[5]=0.0;
    kernel[6]=-1.0;
    kernel[7]=-2.0;
    kernel[8]=-1.0;
    int cols=qin->width();
    int rows=qin->height();    
    struct image imggy=convolve_img(qin);
    kernel[0]=1.0;
    kernel[1]=0.0;
    kernel[2]=-1.0;
    kernel[3]=2.0;
    kernel[4]=0.0;
    kernel[5]=-2.0;
    kernel[6]=1.0;
    kernel[7]=0.0;
    kernel[8]=-1.0;
    struct image imggx=convolve_img(qin);
    sob_sob_arr.R=new int[cols*rows];
    sob_sob_arr.G=new int[cols*rows];
    sob_sob_arr.B=new int[cols*rows];
    sob_sob_arr.dirR=new double[cols*rows];
    sob_sob_arr.dirG=new double[cols*rows];
    sob_sob_arr.dirB=new double[cols*rows];
    int x,y,i;
    for(x=0;x<cols;x++)
        for(y=0;y<rows;y++){
            i=x+y*cols;
            sob_sob_arr.R[i]=(int)sqrt(imggx.R[i]*imggx.R[i]+imggy.R[i]*imggy.R[i]);
            sob_sob_arr.G[i]=(int)sqrt(imggx.G[i]*imggx.G[i]+imggy.G[i]*imggy.G[i]);
            sob_sob_arr.B[i]=(int)sqrt(imggx.B[i]*imggx.B[i]+imggy.B[i]*imggy.B[i]);
            sob_sob_arr.dirR[i]=atan(imggy.R[i]*1.0/imggx.R[i]);
            sob_sob_arr.dirG[i]=atan(imggy.G[i]*1.0/imggx.G[i]);
            sob_sob_arr.dirB[i]=atan(imggy.B[i]*1.0/imggx.B[i]);
    }
    return sob_sob_arr;

}

SobelFilter::~SobelFilter(){
    delete [] kernel;
}
